package com.ita.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.ita.dao.AssessDao;
import com.ita.dao.MUserDao;
import com.ita.dto.AssessDto;
import com.ita.pojo.Assess;
import com.ita.pojo.MUser;
import com.ita.service.AssessService;

@Service(value = "assessService")
public class AssessServiceImpl implements AssessService {

	@Autowired(required = true)
	private MUserDao mDao;
	@Autowired(required = true)
	private AssessDao assessDao;

	@Override
	@Transactional
	public List<Assess> findAllByMidAndPaging(String mid, int max, int begin) {
		
		return assessDao.findAllByPage(mid,max,begin);
	}

	@Override
	@Transactional
	public Assess addClientAssess(AssessDto assertDto) {

		// TODO Auto-generated method stub
		String mid = assertDto.getMid();
		String cname = assertDto.getCname();
		int score = assertDto.getScore();
		String msg = assertDto.getMsg();
//		long assessTime = assertDto.getAssessTime();

		MUser m = mDao.getById(mid);
		if (m == null) {
			return null;
		}
		Assess assess = new Assess();

		assess.setId(UUID.randomUUID().toString());
		assess.setmUser(m);
		assess.setcName(cname);
		assess.setScore(score);
		assess.setMsg(msg);
		assess.setAssess_time(new Date().getTime());

		Assess newAssess = assessDao.addAssess(assess);
		return newAssess;

	}

	@Override
	@Transactional
	public Assess replyToAssess(String reply, String id) {
		// TODO Auto-generated method stub

		long now = System.currentTimeMillis();
		Assess assess = assessDao.updateAssess(id, reply, now);
		return assess;
	}

	@Override
	public List<Assess> findAllByMid(String mid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Assess> findAllNoReplyByMidAndPaging(String mid, int max, int begin) {
		// TODO Auto-generated method stub
		return assessDao.findAllNoReplyByPage(mid,max,begin);
	}

	@Override
	public List<Assess> findAllReplyByMidAndPaging(String mid, int max, int begin) {
		// TODO Auto-generated method stub
		return assessDao.findAllReplyByPage(mid,max,begin);
	}

	@Override
	public List<Integer> initSize(String mid,int max) {
		// TODO Auto-generated method stub
		List<Integer> pages =new ArrayList<Integer>();
		int replySize=assessDao.getReplySize(mid);
		int noReplySize=assessDao.getNotReplySize(mid);
		
		int replyPages=countPages(replySize,max);
		int noReplyPages=countPages(noReplySize,max);
		
		pages.add(replyPages);
		pages.add(noReplyPages);
		return pages;
	}

	public int countPages(int size,int max){
		
		int pages=0;
		int s=size%max==0?0:1;
		pages=s+size/max;
		return pages;
		
	}

}
